<?php
namespace Plu\Library\Services\Tga\Lottery;

use Plu\Library\Services\ServiceBase,
    Plu\Library\Models\Tga\Lottery\Applicants as ApplicantsModel;

/**
 * @usage 
 * $config = array('id'=>, 'field'=>key的字段名, 'prize_level'=>谢谢);
 * $service = new LotteryService($appkey, $uid, $config)
 *
 */
class LotteryService extends ServiceBase {
    //错误代码
    const SUCCESS            = 0;//成功 标识符0
    const ERROR_NONEXIST      = -2;//不存在

    protected $_uid;

    protected $_appkey    = null;

    protected $_verified  = false;//验证通过

    protected $_errorInfo = [];

    protected $id      = null;

    protected $field      = null;

    protected $prize_level = null;


    public function __construct($appkey, $uid, $config) {
        $this->_appkey = $appkey;
        $this->_uid    = $uid;

        $this->id    = $config['id'];
        $this->field = $config['field'];
        $this->prize_level = $config['prize_level'];

        $this->_redis = new RedisService();

        $this->now = time();
    }


    /**
     * 
     */
    public function hasPrizeid(){
        $r = $this->_redis->get($this->_appkey, $this->field);
        if(!$r){
            $info = ApplicantsModel::getItemByUid($this->_appkey, $this->_uid, $this->id, $this->prize_level);
            if(!empty($info)){
                return $info;
            }
            return false;
        }
        return true;
    }
    


    /**
     * 判断prizeid
     */
    public function validate() {
        $info = $this->hasPrizeid();
        if(!$info){
            $this->setErrorMessage(self::ERROR_NONEXIST, 'nonexist', "获奖Id不存在！");
            return false;
        }
        return true;
    }


    public function getErrorMessages(){
        return $this->_errorInfo;
    }

    /**
     *  错误设置与获取
     */
    protected function setErrorMessage($errorCode, $field, $errorInfo){
        $this->_errorInfo[] = array(
            'code'      => $errorCode,
            'field'     => $field,
            'errorInfo' => $errorInfo
        );
    }


    
}
?>
